import {_decorator, Component, Label, Node, Quat, quat, Sprite, tween, v3, Vec3} from 'cc';
import {AudioClipID, AudioManager} from "./AudioManager";
import {GameMain} from "./GameMain";
import {MoneyAnim} from "./MoneyAnim";
import Platform, {PlatformType} from "./Platform";
import {NativeEvt} from "./GameConfig";

const { ccclass, property } = _decorator;

@ccclass('GetMoneyUI')
export class GetMoneyUI extends Component {

    @property({type:MoneyAnim})
    moneyAnim:MoneyAnim=null;

    public targetNode:Node=null;
    public inited:boolean=false;
    private numLabel:Label=null;

    onLoad()
    {
        this.init();
        Platform.createInsertAd();
    }

    public onGetClicked()
    {
        GameMain.instance.setVedioAwardScene(NativeEvt.VedioGetMoneyUI);
        Platform.playVideoAd(() =>
        {
            this.videoAward();
        });
    }

    public videoAward()
    {
        if(GameMain.instance.startGameUI.node.active)
        {
            this.moneyAnim.setDstNode(GameMain.instance.startGameUI.moneySpriteNode);
        }
        else
        {
            this.moneyAnim.setDstNode(GameMain.instance.mainUI.moneyNode);
        }
        this.moneyAnim.play(()=>{

        });

        GameMain.instance.gameData.addMoney(GameMain.instance.getClaimMoneyNum());
        GameMain.instance.mainUI.updateMoneyLabel();
        GameMain.instance.startGameUI.updateMoneyLabel();

        this.scheduleOnce(()=>{
            AudioManager.playClip(AudioClipID.Coin);
        }, 0.3);

        this.scheduleOnce(()=>{
            this.close();
        }, 0.8);
    }

    public init()
    {
        this.targetNode=this.node.getChildByName("window");
        this.numLabel=this.targetNode.getChildByName("numLabel").getComponent(Label);
        this.inited=true;
    }

    start() {

    }

    public show()
    {
        if(this.node.active)
            return;
        if(!this.inited)
            this.init();

        //AudioManager.playClip(AudioClipID.Tip);

        this.node.active=true;
        this.targetNode.setScale(0.4, 0.4,0.4);
        this.numLabel.string="+"+ GameMain.instance.getClaimMoneyNum();
        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .call(()=>
            {
                if(Platform.currentPlatform===PlatformType.VIVO)
                {
                    //Platform.createOVCustomAd();
                }
                else if(Platform.isAndroid())
                {
                    GameMain.instance.showNativeAd();
                }
                else if(Platform.currentPlatform===PlatformType.OPPO && GameMain.instance.sTime)
                {
                    //Platform.createOVCustomAd();
                }
            })
            .start();
    }

    public close()
    {
        //this.node.active=false;
        tween(this.targetNode).to(0.2,{scale:v3(0.5, 0.5, 0.5)}).call(()=>{
            this.node.active=false;
        }).start();
    }

    update(dt: number)
    {
    }
}

